const SumUtils = {
	getSums(data, columns, n, isFormTable) {
		let sums = [];
		columns.forEach((column, index) => {
			if (index === 0) {
				sums[index] = "合计";
				return;
			}
			if (isFormTable) {
				const values = data.map(item => item[column]);
				this.compute(sums, values, index + n, 2);
			} else {
				if (column.isSum) {
					const digits = column.digits ? column.digits : 2;
					const values = data.map(item => Number(item[column.prop]));
					this.compute(sums, values, index + n, digits);
				}
			}
		});
		return sums;
	},

	compute(sums, values, index, digits) {
		if (!values.every(value => isNaN(value))) {
			sums[index] = values.reduce((prev, curr) => {
				const value = Number(curr);
				if (!isNaN(value)) {
					return prev + curr;
				} else {
					return prev;
				}
			}, 0).toFixed(digits);
		} else {
			sums[index] = "";
		}
	}
};
export default SumUtils;
